package nsoft.swing.action;

import java.lang.ref.WeakReference;

import nsoft.util.BeanUtilities;

public class ApplicationAction {
	/**
	 * Reference to controller class.
	 */
	WeakReference<Object> controller;
	/**
	 * Labels of the button for different states.
	 */
	private String name;
	/**
	 * Method names for different states.
	 */
	private String method;
	/**
	 * Tool tip text of the button for different states.
	 */
	private String description;

	public ApplicationAction(String name, String method, Object controller) {
		this(name, method, controller, null);

	}

	public ApplicationAction(String name, String method, Object controller,
			String description) {
		this.name = name;
		this.method = method;
		this.description = description;
		this.controller = new WeakReference<Object>(controller);
	}

	public String getName(int state) {
		return name;
	}

	public String getMethod(int state) {
		return method;
	}

	public String getDescription(int state) {
		return description;
	}

	public void perforAWTAction(ApplicationEvent<?> event) {
		if (!BeanUtilities.invokeMethod(controller.get(), method,
				new Object[] { event })) {
			perforAction();
		}
	}

	/*public void perforAction() {
		BeanUtilities.invokeMethod(controller.get(), method, new Object[] {});
	}*/
	public void perforAction(Object... params ) {
		BeanUtilities.invokeMethod(controller.get(), method, params);
	}
}
